import javax.swing.*;

import java.awt.CardLayout;
import java.awt.Dimension;
import java.util.ArrayList;

public class MainFrame extends JFrame {
    ConnectGTD connect = new ConnectGTD();
    CardLayout c = new CardLayout(0, 0);

    AddUserGUI au = new AddUserGUI();
    EditPatientGUI edit = new EditPatientGUI();
    LoginGUI login = new LoginGUI();
    DoctorGUI doctor = new DoctorGUI();
    NurseGUI nurse = new NurseGUI();
    AdminGUI admin = new AdminGUI();
    DoctorsOrdersGUI Do = new DoctorsOrdersGUI();
    HealthHistoryGUI hh = new HealthHistoryGUI();
    SearchPatientGUI sp = new SearchPatientGUI();
    NewPatientGUI np = new NewPatientGUI();
    TreatmentRecordGUI tr = new TreatmentRecordGUI();
    ViewAppointmentsGUI va = new ViewAppointmentsGUI();
    SearchUserGUI su = new SearchUserGUI();
    EditUserGUI eu = new EditUserGUI();
    PatientGUI pg = new PatientGUI();
    SearchAppointments sa = new SearchAppointments();
    InputDoctorsOrdersGUI id = new InputDoctorsOrdersGUI();

    int role = 0;

    public MainFrame() {

        getContentPane().setLayout(c);

        getContentPane().add("login", login);
        getContentPane().add("doctor", doctor);
        getContentPane().add("nurse", nurse);
        getContentPane().add("admin", admin);
        getContentPane().add("Do", Do);
        getContentPane().add("hh", hh);
        getContentPane().add("sp", sp);
        getContentPane().add("EditP", edit);
        getContentPane().add("np", np);
        getContentPane().add("tr", tr);
        getContentPane().add("va", va);
        getContentPane().add("su", su);
        getContentPane().add("eu", eu);
        getContentPane().add("pg", pg);
        getContentPane().add("sa", sa);
        getContentPane().add("id", id);
        getContentPane().add("au", au);
        getContentPane().add("hh", hh);

        // cards = new JPanel(cl);
        // cards.add(login);
        // cards.add(doctor);

        // cards.remove(login);

        // cl.show(cards, "doctor");

        // this.add(login);
        // this.add(nurse);
        // this.add(doctor);
        // this.add(admin);
        // this.add(edit);

    }

    public void changeScreen() {

        if (pg.getLoc() == 14) {
            c.show(getContentPane(), "sa");
        }
        if (login.getRole() == "patient" && login.getLoc() == 1) {
            connect.setUsername(login.name());
            connect.setPassWord(login.pass());
            connect.setCurrentUser(connect.login(login.getRole()));
            if (connect.getUser().getRoles()[0] == true)
                c.show(getContentPane(), "pg");
        }

        if (doctor.getLoc() == 10 || nurse.getLoc() == 10 || pg.getLoc() == -1
                || admin.getLoc() == 33) {
            login.setRole("");
            c.show(getContentPane(), "login");
            connect = new ConnectGTD();
            au = new AddUserGUI();
            edit = new EditPatientGUI();
            login = new LoginGUI();
            doctor = new DoctorGUI();
            nurse = new NurseGUI();
            admin = new AdminGUI();
            Do = new DoctorsOrdersGUI();
            hh = new HealthHistoryGUI();
            sp = new SearchPatientGUI();
            np = new NewPatientGUI();
            tr = new TreatmentRecordGUI();
            va = new ViewAppointmentsGUI();
            su = new SearchUserGUI();
            eu = new EditUserGUI();
            pg = new PatientGUI();
            sa = new SearchAppointments();
            id = new InputDoctorsOrdersGUI();
        }
        if (sp.getLoc() == 6) {
            connect.setUser(sp.getUsern());
            tr.setUser(connect.getUser());
            c.show(getContentPane(), "tr");
        }
        if (sp.getLoc() == 7 && login.getRole() == "doctor") {
            c.show(getContentPane(), "id");
        }
        if (su.getLoc() == 12) {
            eu.setUser(su.getUsern());
            c.show(getContentPane(), "eu");
        }
        if (tr.getLoc() == 20 && login.getRole() == "doctor") {
            id.setTreatmentRecord(tr.getTreatmentRecord());
            connect.setTreatmentRecord(tr.getTreatmentRecord());
            c.show(getContentPane(), "id");
        }

        if (su.getLoc() == 3) {
            connect.setName(su.getUser());
            su.setUsers(connect.getUserSearch());
        }

        if (np.getLoc() == -1 || hh.getLoc() == -1 || Do.getLoc() == -1
                || eu.getLoc() == -1 || su.getLoc() == -1 || au.getLoc() == -1
                || sa.getLoc() == -1 || edit.getLoc() == -1
                || va.getLoc() == 21 || sp.getLoc() == -1 || tr.getLoc() == -1
                || id.getLoc() == -1)

        {
            if (login.getRole() == "doctor") {
                c.show(getContentPane(), "doctor");
                doctor.setLoc(1);
            }

            if (login.getRole() == "nurse") {
                c.show(getContentPane(), "nurse");
                nurse.setLoc(1);
            }
            if (login.getRole() == "admin") {
                c.show(getContentPane(), "admin");
                admin.setLoc(1);
            }
            if (login.getRole() == "patient") {
                c.show(getContentPane(), "pg");
                pg.setLoc(1);
            }
            np.setLoc(0);
            sp.setLoc(0);

            if (edit.getLoc() == -4) {
                connect.deleteUser();
                edit.setLoc(-1);
            }

            if (eu.getLoc() == -4) {
                connect.deleteUser();
                eu.setLoc(-1);
            }

        }

        if (doctor.getLoc() == 13 || nurse.getLoc() == 13
                || admin.getLoc() == 13) {
            c.show(getContentPane(), "np");
        }

        if (login.getLoc() == 1 && login.getRole() == "doctor") {
            connect.setUsername(login.name());
            connect.setPassWord(login.pass());
            connect.setCurrentUser(connect.login(login.getRole()));
            if (connect.getUser().getRoles()[2] == true) {
                c.show(getContentPane(), "doctor");
            }
        }

        if (login.getRole() == "nurse" && login.getLoc() == 1)
            connect.setUsername(login.name());
        connect.setPassWord(login.pass());
        connect.setCurrentUser(connect.login(login.getRole()));

        {
            if (connect.getUser().getRoles()[1] == true) {
                c.show(getContentPane(), "nurse");
            }
        }
        if (login.getRole() == "admin" && login.getLoc() == 1) {
            connect.setUsername(login.name());
            connect.setPassWord(login.pass());
            connect.setCurrentUser(connect.login(login.getRole()));
            if (connect.getUser().getRoles()[3] == true) {
                c.show(getContentPane(), "admin");
            }
        }

        if (pg.getLoc() == 5) {
            Do.setUser(connect.getCurrentUser());
            c.show(getContentPane(), "Do");
        }

        if (doctor.getLoc() == 2 || nurse.getLoc() == 2 || admin.getLoc() == 2) {
            c.show(getContentPane(), "sp");
        }

        if (id.getLoc() == 1) {
            connect.getUser().addTreatmentRecord(id.getTreatmentRecord());
            connect.editUser();
            id.setLoc(-1);
        }

        if (doctor.getLoc() == 3) {
            c.show(getContentPane(), "Do");
        }

        if (doctor.getLoc() == 4) {
            c.show(getContentPane(), "hh");
        }

        if (sp.getLoc() == 12) {
            doctor.setLoc(0);
            edit.setUser(connect.getUser());
            c.show(getContentPane(), "EditP");
        }

        if (edit.getLoc() == 3) {
            connect.setUser(edit.getUser());
            connect.editUser();
            edit.setLoc(-1);
        }

        if (hh.getLoc() == 14) {
            c.show(getContentPane(), "sp");
        }

        if (doctor.getLoc() == 5 || nurse.getLoc() == 5 || admin.getLoc() == 5) {
            c.show(getContentPane(), "tr");
        }

        if (doctor.getLoc() == 6 || nurse.getLoc() == 6 || admin.getLoc() == 6) {
            c.show(getContentPane(), "va");
        }

        if (admin.getLoc() == 8) {
            c.show(getContentPane(), "su");
        }
        if (su.getLoc() == 7) {
            c.show(getContentPane(), "eu");
        }
        if (sp.getLoc() == 20) {
            connect.setUser(sp.getUsern());
            edit.setUser(sp.getUsern());
            c.show(getContentPane(), "EditP");
        }
        if (eu.getLoc() == 3) {
            connect.setUser(eu.getUser());
            connect.editUser();
            eu.setLoc(-1);
        }

        if (sp.getLoc() == 3) {
            connect.setName(sp.getUser());
            sp.setUsers(connect.getUserSearch());
        }

        if (admin.getLoc() == 12) {
            c.show(getContentPane(), "au");

        }
        if (pg.getLoc() == 20) {
            hh.setUser(connect.getCurrentUser());
            c.show(getContentPane(), "hh");
        }

        if (au.getLoc() == 14) {
            connect.setUser(au.getUser());

            c.show(getContentPane(), "admin");
            System.out.println(connect.getUser().getAddress());

        }
        if (sp.getLoc() == 3) {
            connect.setSearch(sp.getUser());
            sp.setUsers(connect.getUsers());
        }

        if (np.getLoc() == 14) {
            connect.setUser(np.getUser());
            if (login.getRole() == "doctor") {
                c.show(getContentPane(), "doctor");
            } else {
                c.show(getContentPane(), "nurse");
            }
            System.out.println(connect.getUser().getAddress());

        }
        if (tr.getLoc() == 1) {
            connect.setUser(tr.getUser());
            connect.editUser();
            tr.setLoc(-1);
        }

        if (va.getLoc() == 12) {
            connect.setDate(va.getDate());
            va.setAppointments(connect.getOwnAppointments());
            va.setAppointments(connect.getAppointments());
        }
        if (sa.getLoc() == -3) {
            connect.setDate(sa.getDate());
            sa.setApp(connect.getAppointmentsSearch());
            // sa.setApp(connect.getAppointments());
        }

        if (sa.getLoc() == 20) {
            connect.setAppointment(sa.getAppointment());
        }
        if (hh.getLoc() == 2) {
            hh.setUser(connect.getUser());
        }

        if (Do.getLoc() == -2) {
            Do.setUser(connect.getUser());
        }

    }

    public LoginGUI getLogin() {
        return login;
    }

    public DoctorGUI getDoc() {
        return doctor;
    }

    public void reset() {
        edit.setLoc(0);
        doctor.setLoc(0);
        login.setLoc(0);
        nurse.setLoc(0);
        admin.setLoc(0);
        Do.setLoc(0);
        hh.setLoc(0);
        sp.setLoc(0);
        np.setLoc(0);
        va.setLoc(0);
        tr.setLoc(0);
        su.setLoc(0);
        eu.setLoc(0);
        pg.setLoc(0);
        sa.setLoc(0);
        id.setLoc(0);
        au.setLoc(0);

    }

}
